package com.xxx.video.main.fragments;

import android.os.Bundle;

import androidx.recyclerview.widget.ConcatAdapter;
import androidx.recyclerview.widget.LinearLayoutManager;

import com.xxx.video.R;
import com.xxx.video.commen.adapters.BaseRecycleViewAdapter;
import com.xxx.video.commen.fragments.BaseFragment;
import com.xxx.video.commen.utils.ListUtils;
import com.xxx.video.commen.views.recyclerviews.RefreshRecyclerView;
import com.xxx.video.main.activitys.VideoDetailsActivity;
import com.xxx.video.main.adapters.HomeHotTypeListAdapter;
import com.xxx.video.main.adapters.HomeHotVerticalVideoListAdapter;
import com.xxx.video.main.beans.VideoListBean;
import com.xxx.video.main.presenters.HomeHotPresenter;
import com.xxx.video.main.presenters.IHomeHotView;

import java.util.List;
import java.util.Objects;

import butterknife.BindView;

public class HomeHotFragment extends BaseFragment<HomeHotPresenter> implements IHomeHotView, RefreshRecyclerView.OnRecyclerViewRefreshOrLoadMoreListener {
    @BindView(R.id.rcv_home_hot_fragment)
    RefreshRecyclerView rcvHomeHotFragment;
    /**
     * adapter
     */
    private ConcatAdapter mConcatAdapter;
    /**
     * type
     */
    private String type = "2";
    /**
     * page
     */
    private int page = 1;
    /**
     * adapter
     */
    private HomeHotVerticalVideoListAdapter mHotVerticalVideoListAdapter;

    public static HomeHotFragment newInstance(Bundle args) {
        HomeHotFragment fragment = new HomeHotFragment();
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    protected HomeHotPresenter createPresenter() {
        return new HomeHotPresenter(Objects.requireNonNull(getActivity()), this);
    }

    @Override
    public int getContentLayout() {
        return R.layout.fragment_home_hot;
    }

    @Override
    public void initData(Bundle extras) {
    }

    @Override
    public void refreshData(Bundle extras) {

    }

    @Override
    public void onResume() {
        super.onResume();
        if (isFirst) {
            isFirst = false;
            initList();
            getPresenter().getRankList(page, type);
        }
    }

    @Override
    public void showRootLayoutStatus(int status) {
        showLayoutStatus(status);
    }

    @Override
    public void initList() {
        rcvHomeHotFragment.setLayoutManager(new LinearLayoutManager(getActivity()));
        mConcatAdapter = new ConcatAdapter();
        rcvHomeHotFragment.setAdapter(mConcatAdapter);
        HomeHotTypeListAdapter homeHotTypeListAdapter = new HomeHotTypeListAdapter(Objects.requireNonNull(getActivity()));
        mConcatAdapter.addAdapter(homeHotTypeListAdapter);
        homeHotTypeListAdapter.setOnItemClickListener((o, position, type1) -> {
            type = String.valueOf(type1);
            page = 1;
            getPresenter().getRankList(page, type);
        });
        rcvHomeHotFragment.setOnRecyclerViewRefreshOrLoadMoreListener(this);
    }

    @Override
    public void initData2View(List<VideoListBean> rankListBeanList) {
        rcvHomeHotFragment.resetState();
        showLayoutStatus(1);
        if (mHotVerticalVideoListAdapter == null) {
            mHotVerticalVideoListAdapter = new HomeHotVerticalVideoListAdapter(Objects.requireNonNull(getActivity()));
            mConcatAdapter.addAdapter(mHotVerticalVideoListAdapter);
            mHotVerticalVideoListAdapter.setOnItemClickListener((BaseRecycleViewAdapter.OnItemClickListener<VideoListBean>) (hotListBean, position, type) -> startVideoDetailsActivity(hotListBean.fContentId));
        }
        if (page == 1) {
            mHotVerticalVideoListAdapter.setData(rankListBeanList);
        } else {
            mHotVerticalVideoListAdapter.addData(rankListBeanList);
        }
        if (ListUtils.isEmpty(rankListBeanList) || rankListBeanList.size() < 10) {
            rcvHomeHotFragment.setNoMoreData();
        }
    }

    @Override
    public void startVideoDetailsActivity(String id) {
        Bundle bundle = new Bundle();
        bundle.putString("Id", id);
        intent2Activity(VideoDetailsActivity.class, bundle);
    }

    @Override
    public void onRecyclerviewRefresh() {
        page = 1;
        getPresenter().getRankList(page, type);
    }

    @Override
    public void onRecyclerviewLoadMore() {
        page++;
        getPresenter().getRankList(page, type);
    }
}
